package fruit_machine

import (
	"math/rand"
	"sort"
)

// 中奖类型
const (
	AwardTypeNormal    AwardType = 1 //普通
	AwardTypeTrain     AwardType = 2 //开火车
	AwardTypeDsy       AwardType = 3 //大三元
	AwardTypeXsy       AwardType = 4 //小三元
	AwardTypeMtx       AwardType = 5 //满天星
	AwardTypeFullHouse AwardType = 6 //同奖全中
	AwardTypeGrandSlam AwardType = 7 //大满贯
	AwardTypeLeftLuck  AwardType = 8 //左边luck
	AwardTypeRightLuck AwardType = 9 //右边luck

	GameplayNormal = 1 //正常玩法
	GameplayBs     = 2 //大小玩法
	MaxSite        = 24

	Background = "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024031413485087583bg-app.png" //背景图
)

func fruitConfig() ([]*FruitConfig, float64) {
	fruits := []*FruitConfig{
		{1, "大乔丹", []int32{7}, 100, 100, 0, 0, []int{1, 2}, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024031413272519044%E5%96%AC%E4%B8%B9.png", 1},
		{2, "小乔丹", []int32{6}, 50, 50, 0, 0, []int{1, 2}, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024040415152051271%E5%96%AC%E4%B8%B92.png", 1},
		{3, "大科比", []int32{19}, 20, 40, 0, 0, []int{3, 4}, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024031413284450373%E7%A7%91%E6%AF%94.png", 2},
		{4, "小科比", []int32{18}, 3, 3, 0, 0, []int{3, 4}, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024040415154674654%E7%A7%91%E6%AF%942.png", 2},
		{5, "大詹皇", []int32{23}, 20, 30, 0, 0, []int{5, 6}, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024031413294082586%E8%A9%B9%E5%A7%86%E6%96%AF.png", 3},
		{6, "小詹皇", []int32{24}, 3, 3, 0, 0, []int{5, 6}, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024040415162040572%E8%A9%B9%E5%A7%86%E6%96%AF2.png", 3},
		{7, "大库里", []int32{11}, 20, 20, 0, 0, []int{7, 8}, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024031413300749983%E5%BA%AB%E9%87%8C.png", 4},
		{8, "小库里", []int32{12}, 3, 3, 0, 0, []int{7, 8}, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024040415164473553%E5%BA%AB%E9%87%8C2.png", 4},
		{9, "大字母", []int32{5, 17}, 10, 20, 0, 0, []int{9, 10}, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024031413302768305%E5%AD%97%E6%AF%8D%E5%93%A5.png", 5},
		{10, "小字母", []int32{3}, 3, 3, 0, 0, []int{9, 10}, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024040415171333810%E5%AD%97%E6%AF%8D%E5%93%A52.png", 5},
		{11, "大杜兰特", []int32{10, 22}, 10, 15, 0, 0, []int{11, 12}, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024031413305068423%E6%9D%9C%E8%98%AD%E7%89%B9.png", 6},
		{12, "小杜兰特", []int32{21}, 3, 3, 0, 0, []int{11, 12}, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024040415174772609%E6%9D%9C%E8%98%AD%E7%89%B92.png", 6},
		{13, "大东七", []int32{4, 16}, 10, 15, 0, 0, []int{13, 14}, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024031413311170917%E6%9D%B1%E5%A5%91%E5%A5%87.png", 7},
		{14, "小东七", []int32{15}, 3, 3, 0, 0, []int{11, 12}, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024040415180970337%E6%9D%B1%E5%A5%91%E5%A5%872.png", 7},
		{15, "大威少", []int32{2, 8, 14, 20}, 5, 5, 0, 0, []int{15, 16}, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024031413312852147%E5%A8%81%E5%B0%91.png", 8},
		{16, "小威少", []int32{9}, 3, 3, 0, 0, []int{15, 16}, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024040415182929429%E5%A8%81%E5%B0%912.png", 8},
		{17, "左luck", []int32{1}, 0, 0, 0, 0, []int{1}, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024031616071950512luck-left.png", 0},
		{18, "右luck", []int32{13}, 0, 0, 0, 0, []int{13}, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024031616075740609luck-right.png", 0},
	}

	totalOdds := 0.0
	for _, f := range fruits {
		totalOdds += float64((f.MinOdds + f.MaxOdds) / 2)
	}
	// 计算累积概率
	totalProbability := 0.0
	for k, f := range fruits {
		if f.MaxOdds > 0 {
			probability := (float64(totalOdds) / float64((f.MinOdds+f.MaxOdds)/2) / float64(totalOdds)) * 100
			fruits[k].probability = probability
		}
	}

	// 按照概率排序
	sort.Slice(fruits, func(i, j int) bool {
		return fruits[i].probability < fruits[j].probability
	})

	for k, f := range fruits {
		if f.probability > 0 {
			totalProbability += f.probability
			fruits[k].CumProbability = totalProbability
		}

	}

	return fruits, totalProbability

}

func betAreaConfig() []*BetAreaConfig {
	betArea := []*BetAreaConfig{
		{1, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024031413272519044%E5%96%AC%E4%B8%B9.png", []int{1, 2}},
		{2, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024031413284450373%E7%A7%91%E6%AF%94.png", []int{3, 4}},
		{3, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024031413294082586%E8%A9%B9%E5%A7%86%E6%96%AF.png", []int{5, 6}},
		{4, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024031413300749983%E5%BA%AB%E9%87%8C.png", []int{7, 8}},
		{5, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024031413302768305%E5%AD%97%E6%AF%8D%E5%93%A5.png", []int{9, 10}},
		{6, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024031413305068423%E6%9D%9C%E8%98%AD%E7%89%B9.png", []int{11, 12}},
		{7, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024031413311170917%E6%9D%B1%E5%A5%91%E5%A5%87.png", []int{13, 14}},
		{8, "https://liveimages-1318974551.cos.accelerate.myqcloud.com/images/2024031413313619831%E5%A8%81%E5%B0%912.png", []int{15, 16}},
	}
	return betArea
}

func fruitAwardTypeConfig() []*AwardTypeConfig {
	awardTypes := []*AwardTypeConfig{
		{AwardTypeGrandSlam, 1}, //大满贯
		{AwardTypeMtx, 2},       //满天星
		{AwardTypeDsy, 4},       //大三元
		{AwardTypeFullHouse, 6}, //同奖全中
		{AwardTypeTrain, 9},     //开火车
		{AwardTypeXsy, 15},      //小三元
		{AwardTypeLeftLuck, 35},
		{AwardTypeRightLuck, 45},
		{AwardTypeNormal, 200},
	}
	return awardTypes
}

type AwardType int

func (f *Fruits) getAwardType() AwardType {
	r := rand.Float64() * 120
	for _, v := range f.AwardTypes {
		if r < v.Probability {
			return v.AwardTypeIdx
		}
	}
	return AwardTypeNormal
}

func (f *Fruits) getAwardConfig(idx int) *FruitConfig {
	fruitAwardConfig := &FruitConfig{}
	for _, v := range f.Fruit {
		if v.Idx == idx {
			fruitAwardConfig = v
			break
		}
	}
	return fruitAwardConfig
}
